//
//  ApplyPersonInfoCell.swift
//  LongTermInsurance
//
//  Created by zolobdz on 2018/11/28.
//  Copyright © 2018年 HeChao. All rights reserved.
//

import UIKit

class ApplyPersonInfoCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var textField: UITextField!
    
    @IBOutlet weak var chooseButton: UIButton!
    
    
    lazy var picker: CDInfoPickerView = {
        let picker = CDInfoPickerView()
        picker.isHidden = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(viewControllerEndEditing))
        picker.addGestureRecognizer(tap)
        picker.confirmButton.addTarget(self, action: #selector(pickerConfirmAction), for: .touchUpInside)
        return picker
    }()
    
    @objc func pickerConfirmAction() {
        
        guard model.type == .choose else {
            return
        }

        model.chooseModel = picker.result as! ApplyChooseModel
        model.detailText.value = model.chooseModel.name
        
        chooseButton.isSelected = true
        chooseButton.setTitle(model.detailText.value, for: .selected)
        
        picker.hidePickerView()
    }
    
    @objc func viewControllerEndEditing(){
        UIApplication.shared.sendAction(#selector(resignFirstResponder), to: nil, from: nil, for: nil)
        if !picker.isHidden {
            picker.hidePickerView()
        }
    }
    
    var model: ApplyPersonInfo! {
        willSet {
            if (newValue != nil) {
                titleLabel.text = newValue.titleName
                switch (newValue.type) {
                case .base:
                    break
                case .enter:
                    textField.isHidden = false
                    chooseButton.isHidden = true
                    textField.placeholder = newValue.placeholder
                    textField.text = newValue.detailText.value
                case .choose:
                    textField.isHidden = true
                    chooseButton.isHidden = false
                    textField.placeholder = ""
                    
                    chooseButton.isSelected = newValue.chooseModel.name.count > 0
                    chooseButton.setTitle(newValue.detailText.value, for: .selected)
                }
                
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        clipsToBounds = true
        _ = textField.rx.text.subscribe(onNext: {[weak self] (str) in
            if let strongSelf = self,strongSelf.model != nil  {
                strongSelf.model.detailText.value = (str ?? "")
            }
        }, onError: nil, onCompleted: nil, onDisposed: nil)
        
        chooseButton.addTarget(self, action: #selector(chooseAction), for: .touchUpInside)
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        selectionStyle = .none
        // Configure the view for the selected state
    }
    
    @objc func chooseAction() {
        
        guard model.type == .choose else {
            return
        }
        
        guard !model.chooseArray.isEmpty else {
            model.getData(model.chooseType)
            return
        }
        
        UIApplication.shared.sendAction(#selector(resignFirstResponder), to: nil, from: nil, for: nil)
        picker.firstInfoArray = model.chooseArray
        picker.showPickerView()
    }
    
    
}
